include ../Makefile.inc

CPPFLAGS += -I$(PILO_DIR)/core

TmpOutDir = $(PILO_PROJECT_DIR)/$(ARCH)

CoreSrcDir = $(PILO_DIR)/core
LibCoreName = core
LibCoreStatic = libpilo$(LibCoreName)_$(ARCH).a
LibCoreShared = libpilo$(LibCoreName)_$(ARCH).so

CoreSources := $(shell find -P $(CoreSrcDir) -name "*.cpp" -exec basename {} \;)
CoreObjects := $(CoreSources:.cpp=.o)
CoreDepends := $(CoreObjects:.o=.d)

VPATH := $(CoreSrcDir)/common:$(CoreSrcDir)/containers:$(CoreSrcDir)/information:$(CoreSrcDir)/ipc:$(CoreSrcDir)/memory:$(CoreSrcDir)/memory/buffer:$(CoreSrcDir)/string:$(CoreSrcDir)/thread 

	
core : $(CoreObjects)
	@if [ ! -d $(TmpOutDir) ] ; \
        then \
                echo "Creating output dir -> $(TmpOutDir) ..."; \
                $(MKDIR) -p $(TmpOutDir); \
        fi
	$(AR) -rv $(TmpOutDir)/$(LibCoreStatic)  $^

depend : $(CoreDepends)
$(CoreDepends): %.d:%.cpp
	@set -e; rm -f $@; \
	$(CC) -MM $(CPPFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

sinclude $(CoreDepends)


.PHONY: install clean

install:
	$(INSTALL) -D $(TmpOutDir)/$(LibCoreStatic) $(OUTPUT_LIB_DIR)/
	
	@if [ ! -d $(OUTPUT_INC_DIR)/core/common ] ; \
        then \
                $(MKDIR) -p $(OUTPUT_INC_DIR)/core/common; \
        fi
	$(INSTALL) $(CoreSrcDir)/common/*.h $(OUTPUT_INC_DIR)/core/common/
	
	
	@if [ ! -d $(OUTPUT_INC_DIR)/core/information ] ; \
	then \
		$(MKDIR) -p $(OUTPUT_INC_DIR)/core/information; \
        fi
	$(INSTALL) $(CoreSrcDir)/information/*.h $(OUTPUT_INC_DIR)/core/information/

	@if [ ! -d $(OUTPUT_INC_DIR)/core/containers ] ; \
	then \
		$(MKDIR) -p $(OUTPUT_INC_DIR)/core/containers; \
	fi
	$(INSTALL) $(CoreSrcDir)/containers/*.h $(OUTPUT_INC_DIR)/core/containers/

	@if [ ! -d $(OUTPUT_INC_DIR)/core/ipc ] ; \
	then \
		$(MKDIR) -p $(OUTPUT_INC_DIR)/core/ipc; \
	fi
	$(INSTALL) $(CoreSrcDir)/ipc/*.h $(OUTPUT_INC_DIR)/core/ipc/

	@if [ ! -d $(OUTPUT_INC_DIR)/core/memory ] ; \
	then \
		$(MKDIR) -p $(OUTPUT_INC_DIR)/core/memory; \
	fi
	$(INSTALL) $(CoreSrcDir)/memory/*.h $(OUTPUT_INC_DIR)/core/memory/

	@if [ ! -d $(OUTPUT_INC_DIR)/core/memory/buffer ] ; \
	then \
		$(MKDIR) -p $(OUTPUT_INC_DIR)/core/memory/buffer; \
	fi
	$(INSTALL) $(CoreSrcDir)/memory/buffer/*.h $(OUTPUT_INC_DIR)/core/memory/buffer/

	@if [ ! -d $(OUTPUT_INC_DIR)/core/string ] ; \
	then \
		$(MKDIR) -p $(OUTPUT_INC_DIR)/core/string; \
	fi
	$(INSTALL) $(CoreSrcDir)/string/*.h $(OUTPUT_INC_DIR)/core/string/

	@if [ ! -d $(OUTPUT_INC_DIR)/core/thread ] ; \
	then \
		$(MKDIR) -p $(OUTPUT_INC_DIR)/core/thread; \
	fi
	$(INSTALL) $(CoreSrcDir)/thread/*.h $(OUTPUT_INC_DIR)/core/thread/

	@if [ ! -d $(OUTPUT_INC_DIR)/core/Core ] ; \
	then \
		$(MKDIR) -p $(OUTPUT_INC_DIR)/core/Core; \
	fi
	$(INSTALL) $(CoreSrcDir)/Core/*.h $(OUTPUT_INC_DIR)/core/Core/

#	tree $(OUTPUT_INC_DIR) > $(OUTPUT_INC_DIR)/headertree.txt


clean:
	$(RM) -f *.d.*
	$(RM) -f $(CoreDepends)
	$(RM) -f $(CoreObjects)
	$(RM) -f $(OUTPUT_LIB_DIR)/$(LibCoreStatic)
	$(RM) -f $(TmpOutDir)/$(LibCoreStatic)
	find $(OUTPUT_INC_DIR)/core -name *.h -exec $(RM) -f {} \;

debug : 
	echo $(CoreObjects) 
	echo "---------------------------"
	echo $(CoreSources)
	echo "---------------------------"
	echo $(CoreDepends)
	echo "----------------------------"
	echo $(VPATH)
	echo "-----------------------------"
	echo $(PILO_DIR)
	echo "-----------------------------"
	echo $(PILO_PROJECT_DIR)
	echo "-----------------------------"
	echo $(PILO_SOLUTION_DIR)


